<%
'#################################################################################
'## UnWritten Open Source Blog Engine
'#################################################################################
'## Copyright (C) 2008 Luigi Violin
'##
'## This program is free software: you can redistribute it and/or modify
'## it under the terms of the GNU General Public License as published by
'## the Free Software Foundation, either version 2 of the License, or
'## (at your option) any later version.
'##
'## This program is distributed in the hope that it will be useful,
'## but WITHOUT ANY WARRANTY; without even the implied warranty of
'## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'## GNU General Public License for more details.
'##
'## You should have received a copy of the GNU General Public License
'## along with this program.  If not, see <http://www.gnu.org/licenses/>.
'##
'## You can obtain support from our forums at:
'##     <http://www.unwrittenblog.com/forums/>
'## Contact the author directly:
'##     <zaamit@hotmail.com/>
'##
'#################################################################################
%>
<%
function gen_recentposts()
	gen_recentposts = "<div id=""sidebar_box"">"
	gen_recentposts = gen_recentposts & "<div id=""sidebar_top"">Recent Posts</div>"
	gen_recentposts = gen_recentposts & "<div id=""sidebar_content"">"
	gen_recentposts = gen_recentposts & "<ul>"
	dbRS2.open "select ent_name,title from " & db_prefix & "posts where publish_status = 1 and ent_type = 1 order by pdate desc limit 5",dbConn
	do while not dbRS2.eof
		gen_recentposts = gen_recentposts & "<li><a href=""/?pg=" & dbRS2("ent_name") & """>" & left(dbRS2("title"),30) & "</a></li>"
		dbRS2.movenext
	loop
	dbRS2.close
	gen_recentposts = gen_recentposts & "</ul>"
	gen_recentposts = gen_recentposts & "</div>"
	gen_recentposts = gen_recentposts & "<div id=""sidebar_bottom""></div>"
	gen_recentposts = gen_recentposts & "</div>"
end function

function gen_mostpopularposts()
	gen_mostpopularposts = "<div id=""sidebar_box"">"
	gen_mostpopularposts = gen_mostpopularposts & "<div id=""sidebar_top"">Most Popular</div>"
	gen_mostpopularposts = gen_mostpopularposts & "<div id=""sidebar_content"">"
	gen_mostpopularposts = gen_mostpopularposts & "<ul>"
	dbRS2.open "select ent_name,title from " & db_prefix & "posts where publish_status = 1 and ent_type = 1 order by views desc limit 5",dbConn
	do while not dbRS2.eof
		gen_mostpopularposts = gen_mostpopularposts & "<li><a href=""/?pg=" & dbRS2("ent_name") & """>" & left(dbRS2("title"),30) & "</a></li>"
		dbRS2.movenext
	loop
	dbRS2.close
	gen_mostpopularposts = gen_mostpopularposts & "</ul>"
	gen_mostpopularposts = gen_mostpopularposts & "</div>"
	gen_mostpopularposts = gen_mostpopularposts & "<div id=""sidebar_bottom""></div>"
	gen_mostpopularposts = gen_mostpopularposts & "</div>"
end function

function gen_mostcommentedposts()
	gen_mostcommentedposts = "<div id=""sidebar_box"">"
	gen_mostcommentedposts = gen_mostcommentedposts & "<div id=""sidebar_top"">Most Commented</div>"
	gen_mostcommentedposts = gen_mostcommentedposts & "<div id=""sidebar_content"">"
	gen_mostcommentedposts = gen_mostcommentedposts & "<ul>"
	dbRS2.open "select ent_name,title,count(" & db_prefix & "comments.id) as comments from " & db_prefix & "posts, " & db_prefix & "comments where " & db_prefix & "posts.id = " & db_prefix & "comments.postid and publish_status = 1 and ent_type = 1 group by ent_name,title order by comments desc limit 5",dbConn
	do while not dbRS2.eof
		gen_mostcommentedposts = gen_mostcommentedposts & "<li><a href=""/?pg=" & dbRS2("ent_name") & """>" & left(dbRS2("title"),30) & "</a></li>"
		dbRS2.movenext
	loop
	dbRS2.close
	gen_mostcommentedposts = gen_mostcommentedposts & "</ul>"
	gen_mostcommentedposts = gen_mostcommentedposts & "</div>"
	gen_mostcommentedposts = gen_mostcommentedposts & "<div id=""sidebar_bottom""></div>"
	gen_mostcommentedposts = gen_mostcommentedposts & "</div>"
end function

function gen_tagcloud()
	gen_tagcloud = "<div id=""sidebar_box"">"
	gen_tagcloud = gen_tagcloud & "<div id=""sidebar_top"">Tag Cloud</div>"
	gen_tagcloud = gen_tagcloud & "<div id=""sidebar_content"">"
	dbRS2.open "select max(numtags),min(numtags) from(select count(tagid) as numtags from " & db_prefix & "posttags," & db_prefix & "tags where tagid = " & db_prefix & "tags.id group by tagid) t1",dbConn
	if isnull(dbRS2(0)) then
		maxt = 0
		else
		maxt = cint(dbRS2(0))
	end if
	if isnull(dbRS2(1)) then
		mint = 0
		else
		mint = cint(dbRS2(1))
	end if
	tinc = (maxt - mint) / 4
	if tinc = 0 then tinc = 1

	dbRS2.close
	dbRS2.open "select " & db_prefix & "tags.text,count(tagid) as nrposts from " & db_prefix & "posttags," & db_prefix & "tags," & db_prefix & "posts where tagid = " & db_prefix & "tags.id and " & db_prefix & "posttags.postid = " & db_prefix & "posts.id and " & db_prefix & "posts.ent_type = 1 group by tagid order by text",dbConn
	do while not dbRS2.eof
		'gen_tagcloud = gen_tagcloud & "<span class=""tag_" & 1 + int(cint(dbRS2("nrposts")) / tinc) & """><a href=""/search.asp?s=" & dbRS2("text") & """>" & dbRS2("text") & "(" & dbRS2("nrposts") & ")</a></span> "
	  if cint(dbRS2("nrposts")) > 0 then
		gen_tagcloud = gen_tagcloud & "<span class=""tag_" & 1 + int((cint(dbRS2("nrposts"))-mint) / tinc) & """><a href=""/search.asp?s=" & dbRS2("text") & """>" & dbRS2("text") & "</a></span> &nbsp; "
	  end if
		dbRS2.movenext
	loop
	dbRS2.close
	gen_tagcloud = gen_tagcloud & "</div>"
	gen_tagcloud = gen_tagcloud & "<div id=""sidebar_bottom""></div>"
	gen_tagcloud = gen_tagcloud & "</div>"
end function

function gen_poll
	Dim p_id,p_title,p_intro,votes,totvotes
	gen_poll = "<div id=""sidebar_box"">"
	gen_poll = gen_poll & "<div id=""sidebar_top"">Current Poll</div>"
	gen_poll = gen_poll & "<div id=""sidebar_content"">" & vbcrlf
	'gen_poll = gen_poll & request.form("poll_option") & "<br />"
	'get active poll
	dbRS2.open "select id,title,intro from " & db_prefix & "polls where status = 1 order by home,date desc",dbConn
	if not dbRS2.eof then
	p_id = dbRS2("id")
	p_title = dbRS2("title")
	p_intro = dbRS2("intro")
	'get total votes for poll
	dbRS2.close
	dbRS2.open "select sum(votes) from " & db_prefix & "polls_options where pollid = " & p_id & " group by pollid",dbConn
	totvotes = dbRS2(0)
	if isnull(totvotes) then totvotes = 0
	totvotes = cint(totvotes)
	if totvotes = 0 then totvotes = 1
	'get poll options
	dbRS2.close
	dbRS2.open "select * from " & db_prefix & "polls_options where pollid = " & p_id,dbConn
	gen_poll = gen_poll & "<form id=""poll"" name=""poll"" action="""" method=""post"" />"
	gen_poll = gen_poll & "<strong>" & p_title & "</strong><br />"
	gen_poll = gen_poll & p_intro & "<hr>"
	i = 1
	do while not dbRS2.eof
		votes = dbRS2("votes")
		if isnull(votes) then votes = 0
		gen_poll = gen_poll & "<input type=""radio"" name=""poll_option"" value=""" & dbRS2("id") & """"
		'if Session("voted") then gen_poll = gen_poll & " disabled=""disabled"" "
		gen_poll = gen_poll & ">&nbsp;" & dbRS2("text") & "<br />"
		gen_poll = gen_poll & "<div class=""poll-opt-" & i & """ style=""width:" & cint(180*cint(votes)/totvotes) & "px;"">&nbsp;</div>"
		if request.form("poll_option") = cstr(dbRS2("id")) and not(Session("voted")) then
			dbConn.execute "update " & db_prefix & "polls_options set votes = " & cint(votes) + 1 & " where id = " & dbRS2("id")
			Session("voted") = true
		end if
		dbRS2.movenext
		i = i + 1
	loop
	dbRS2.close
	if not(Session("voted")) then
		gen_poll = gen_poll & "<center><input type=""submit"" value=""Vote"" /></center>"
		else
			gen_poll = gen_poll & "<font color=""990000""><i>You already voted.</i></font>"
	end if
	gen_poll = gen_poll & "</form>"
	else
	 dbRS2.close
	 gen_poll = gen_poll & "No active poll found."
	end if
	gen_poll = gen_poll & "</div>" & vbcrlf
	gen_poll = gen_poll & "<div id=""sidebar_bottom""></div>"
	gen_poll = gen_poll & "</div>"
end function
%>